package org.intertwine.rssbot;

import java.io.*;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: Stian Lindhom
 * Date: 21.feb.2010
 * Time: 16:34:16
 * To change this template use File | Settings | File Templates.
 */
public class BasicFeedManager implements FeedManager, Serializable {

    private Map<String, Feed> feedList = new HashMap<String, Feed>();
    private RssReaders rs;
    private static final String RSSBOT_DIR = System.getProperty("user.dir") + "/.rssbot/";
    private static final String FEED_FILE = "feeds.dat";

    public BasicFeedManager(RssReaders rs) {
        this.rs = rs;
        load();
        for (Feed feed : feedList.values()) {
            addFeed(feed);
        }
    }

    public Collection<Feed> listFeeds() {
        return feedList.values();
    }

    public void addFeed(String url) {
        Feed f = new Feed(url);
        if (feedList.put(url, f) != null) {
            rs.onFeedAdded(f);
        }
    }

    public void addFeed(Feed f) {
        if (feedList.put(f.getFeedUrl(), f) == null) {
            rs.onFeedAdded(f);
        }
    }

    public void addFeed(String url, String username, String password) {
        Feed f = new Feed(url, username, password);
        if (feedList.put(url, f) != null) {
            rs.onFeedAdded(f);
        }
    }

    public void removeFeed(String url) {
        Feed f = feedList.remove(url);
        if (f != null) {
            rs.onFeedRemoved(f);
        }
    }

    public void load() {
        try {
            FileInputStream fis = new FileInputStream(RSSBOT_DIR + FEED_FILE);
            ObjectInputStream ois = new ObjectInputStream(fis);
            Collection<Feed> feeds = (List<Feed>) ois.readObject();
            for (Feed feed : feeds) {
                addFeed(feed);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (ClassNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    public void save() {
        try {
            File f = new File(RSSBOT_DIR);
            if (!f.exists()) {
                f.mkdir();
            }
            FileOutputStream fos = new FileOutputStream(RSSBOT_DIR + FEED_FILE);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(new ArrayList(this.listFeeds()));
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

}
